package cn.datawin.routeserver.config;

import cn.datawin.routeserver.input.service.InputDealMachineService;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.nio.charset.StandardCharsets;

import static cn.datawin.constant.RocketMqConstant.CMPP_INPUT_TOPIC;

@Slf4j
@Configuration
public class RocketMQConfig {

    private final Environment environment;

    public RocketMQConfig(Environment environment) {
        this.environment = environment;
    }

    /**
     * 配置生产者
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public DefaultMQProducer defaultMQProducer() {
        String nameServer = environment.getProperty("spring.rocketmq.name-server");
        String producerGroup = environment.getProperty("spring.rocketmq.producer.group");

        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(nameServer);
        producer.setVipChannelEnabled(false);

        log.info("RocketMQ Producer 初始化完成，NameServer: {}", nameServer);
        return producer;
    }

}
